//
//  QSMainTabBarController.m
//  SinaWeiBo
//
//  Created by apple on 16/5/27.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "QSMainTabBarController.h"
#import "QSMainTabBar.h"
#import "QSHomeViewController.h"
#import "QSDiscoverViewController.h"
#import "QSMessageViewController.h"
#import "QSProfileViewController.h"
#import "QSBaseNavigationViewController.h"
#import "QSMainTabBar.h"

@interface QSMainTabBarController ()

@end

@implementation QSMainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建自定义的tabBar
    QSMainTabBar *mainTabBar = [[QSMainTabBar alloc] initWithFrame:self.tabBar.frame];
    
    // 关联按钮点击事件
    [mainTabBar.plusButton addTarget:self action:@selector(plushButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 由于原生的tabBar是只读的，必须有kvc来赋值
    [self setValue:mainTabBar forKeyPath:@"tabBar"];
    
    // 设置TabBar
    [self setupUI];
    
    // 初始化子控制器
    [self initControllers];
}

/**
 *  加号按钮的点击事件
 */
- (void)plushButtonClick
{
    QSLog(@"dkgdgdg");
}

/**
 *  设置TabBar
 */
- (void)setupUI
{
    // 1.修改tabbar背景颜色
    UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
    bgView.backgroundColor = [UIColor darkGrayColor];
    // 添加到tabbar上
    [self.tabBar insertSubview:bgView atIndex:0];
    
    // 设置tabbar是否透明
    self.tabBar.opaque = YES;
    
    // 2. 修改tabbar按钮字体颜色(直接去AppDelegate设置主题颜色就可以了)
//    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
//    
//    [[UIBarButtonItem appearance] setTintColor:[UIColor orangeColor]];
//    
//    [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
}

/**
 *  初始化子控制器
 */
- (void)initControllers
{
    QSHomeViewController *homeVc = [[QSHomeViewController alloc] init];
    QSMessageViewController *messageVc = [[QSMessageViewController alloc] init];
    QSDiscoverViewController *discoverVc = [[QSDiscoverViewController alloc] init];
    QSProfileViewController *profileVc = [[QSProfileViewController alloc] init];
    
    [self addSubViewController:homeVc withTitle:@"首页" image:@"tabbar_home" andSelectedImage:@"tabbar_home_highlighted"];
    [self addSubViewController:messageVc withTitle:@"消息" image:@"tabbar_message_center" andSelectedImage:@"tabbar_message_center_highlighted"];
    [self addSubViewController:discoverVc withTitle:@"发现" image:@"tabbar_discover" andSelectedImage:@"tabbar_discover_highlighted"];
    [self addSubViewController:profileVc withTitle:@"我" image:@"tabbar_profile" andSelectedImage:@"tabbar_profile_highlighted"];
    
}

#pragma mark - 添加子控制器
- (void)addSubViewController:(UITableViewController *)vc withTitle:(NSString *)title image:(NSString *)imageName andSelectedImage:(NSString *)selectedImageName
{
    // 在子控制器外面在套上一个导航控制器
    // 创建导航控制器
    QSBaseNavigationViewController *nav = [[QSBaseNavigationViewController alloc] initWithRootViewController:vc];
    
    // 设置子控制器的各个属性
    vc.title = title;
    // 设置tabBarItem的图片，并设置为原始图片样式
    vc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 添加子控制器
    [self addChildViewController:nav];
    
}
@end
